package com.css.fxfzypg.modules.suggestinfo.manage.disasterscenarioManage.repository.impl;

import com.css.fxfzypg.base.dict.repository.entity.DictItemEntity;
import com.css.fxfzypg.modules.suggestinfo.analysis.suggestionanalysis.vo.ParamsVo;
import com.css.fxfzypg.modules.suggestinfo.manage.disasterscenarioManage.repository.DisasterScenarioManageRepository;
import com.css.fxfzypg.modules.suggestinfo.manage.disasterscenarioManage.repository.entity.DisasterScenarioManageEntity;
import com.css.fxfzypg.modules.suggestinfo.manage.vo.ParamVo;
import com.css.fxfzypg.util.PlatformObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class DisasterScenarioManageRepositoryImpl implements DisasterScenarioManageRepository {

    @Autowired
    JdbcTemplate jdbcTemplate;
    @Autowired
    NamedParameterJdbcTemplate namedParameterJdbcTemplate;


    @Override
    public boolean save(DisasterScenarioManageEntity disasterScenarioManageEntity) {
        String sql = "insert into ypg_disaster_scenario_manage values(:id,:province,:provinceId,:city,:cityId,:area,:areaId," +
                ":buildingDamage,:casualties,:geologicHazard,:secondaryDisaster,:earthquakeTraffic,:otherDisaster," +
                ":delFlag,:createId,:createTime,:updateId,:updateTime)";
        SqlParameterSource source = new BeanPropertySqlParameterSource(disasterScenarioManageEntity);
        return namedParameterJdbcTemplate.update(sql, source) > 0;
    }

    @Override
    public Map<String, Object> getDataList(ParamVo paramVo, int curPage, int pageSize) {
        StringBuilder sql = new StringBuilder("select id,province,province_id,city,city_id,area,area_id from ypg_disaster_scenario_manage ");
        StringBuilder whereSql = new StringBuilder("where del_flag = '0' ");
        if (!PlatformObjectUtils.isEmpty(paramVo.getProvinceId())) {
            whereSql.append(" and province_id = '" + paramVo.getProvinceId() + "\'");
        }
        if (!PlatformObjectUtils.isEmpty(paramVo.getCityId())) {
            whereSql.append(" and city_id = '" + paramVo.getCityId() + "\'");
        }
        if (!PlatformObjectUtils.isEmpty(paramVo.getAreaId())) {
            whereSql.append(" and area_id = '" + paramVo.getAreaId() + "\'");
        }
        int firstIndex = (curPage - 1) * pageSize;
        int lastIndex = pageSize;
        StringBuilder orderSql = new StringBuilder(" order by create_time desc");
        StringBuilder limitSql = new StringBuilder(" limit " + lastIndex + " offset " + firstIndex);
        StringBuilder sql1 = sql.append(whereSql);
        Integer total = jdbcTemplate.query(sql1.toString(), new BeanPropertyRowMapper<>(ParamVo.class)).size();
        List<ParamVo> annualRiskManageEntityList = jdbcTemplate.query(sql1.append(orderSql).append(limitSql).toString(), new BeanPropertyRowMapper<>(ParamVo.class));
        Map<String, Object> map = new HashMap();
        map.put("total", total);
        map.put("rows", annualRiskManageEntityList);
        return map;
    }

    @Override
    public DisasterScenarioManageEntity getDataById(String id) {
        String sql = "select * from ypg_disaster_scenario_manage where  del_flag = '0' and id= ?";
        try {
            DisasterScenarioManageEntity disasterScenarioManageEntity = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(DisasterScenarioManageEntity.class), id);
            return disasterScenarioManageEntity;
        } catch (EmptyResultDataAccessException e) {
            return null;
        }
    }

    @Override
    public boolean update(DisasterScenarioManageEntity disasterScenarioManageEntity) {
        String sql = "update ypg_disaster_scenario_manage set province=:province,province_id=:provinceId,city=:city,city_id=:cityId,area=:area,area_id=:areaId," +
                "building_damage=:buildingDamage,casualties=:casualties,geologic_hazard=:geologicHazard,secondary_disaster=:secondaryDisaster," +
                "earthquake_traffic=:earthquakeTraffic,other_disaster=:otherDisaster," +
                "update_id=:updateId,update_time=:updateTime where id = '" + disasterScenarioManageEntity.getId() + "'";
        SqlParameterSource source = new BeanPropertySqlParameterSource(disasterScenarioManageEntity);
        return namedParameterJdbcTemplate.update(sql, source) > 0;
    }

    @Override
    public boolean delete(String id) {
        String sql = "update ypg_disaster_scenario_manage set del_flag = '1' where id = ?";
        return jdbcTemplate.update(sql, id) > 0;
    }

    @Override
    public int getCount(String provinceId, String cityId, String areaId) {
        String sql = "select count(*) from ypg_disaster_scenario_manage where del_flag = '0' and province_id = ? and city_id = ? and area_id =? ";
        Integer count = jdbcTemplate.queryForObject(sql, Integer.class, provinceId, cityId, areaId);
        return count;
    }

    @Override
    public String getId(String provinceId, String cityId, String areaId) {
        String sql = "select id from ypg_disaster_scenario_manage where del_flag = '0' and province_id = ? and city_id = ? and area_id =? ";
        String id = jdbcTemplate.queryForObject(sql, String.class, provinceId, cityId, areaId);
        return id;
    }

    @Override
    public DisasterScenarioManageEntity getDisasterScenarioManageData(ParamsVo paramsVo) {
        try {
            String sql = "select * from ypg_disaster_scenario_manage where  del_flag = '0' and province_id = ? and city_id = ? and area_id =? ";
            DisasterScenarioManageEntity disasterScenarioManageEntity = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(DisasterScenarioManageEntity.class), paramsVo.getProvinceId(), paramsVo.getCityId(), paramsVo.getAreaId());
            return disasterScenarioManageEntity;
        } catch (EmptyResultDataAccessException e) {
            return null;
        }
    }

    @Override
    public List<DisasterScenarioManageEntity> getDisasterScenarioManageAreaData(ParamsVo paramsVo) {
        StringBuilder sql = new StringBuilder("select * from ypg_disaster_scenario_manage ");
        StringBuilder whereSql = new StringBuilder("where del_flag = '0' and province_id = '" + paramsVo.getProvinceId() + "' and city_id = '" + paramsVo.getCityId() + "'");
        List<DisasterScenarioManageEntity> disasterScenarioEntities = jdbcTemplate.query(sql.append(whereSql).toString(), new BeanPropertyRowMapper<>(DisasterScenarioManageEntity.class));
        return disasterScenarioEntities;
    }

    @Override
    public List<DisasterScenarioManageEntity> getDisasterScenarioManageAreaData(String province, String city, List<String> areaList) {
        StringBuilder sql = new StringBuilder("select * from ypg_disaster_scenario_manage ");
        StringBuilder whereSql = new StringBuilder(" where del_flag = '0' and province = :province and city = :city and area in (:areaList)");
        Map map = new HashMap();
        map.put("province", province);
        map.put("city", city);
        map.put("areaList", areaList);
        List query = namedParameterJdbcTemplate.query(sql.append(whereSql).toString(), map, new BeanPropertyRowMapper<>(DisasterScenarioManageEntity.class));
        return query;
    }

    @Override
    public DisasterScenarioManageEntity getByProvinceCityCounty(String provinceId, String cityId, String areaId) {
        try {
            String sql = "select * from ypg_disaster_scenario_manage where province_id = '" + provinceId + "' and city_id = '" + cityId + "' and area_id = '" + areaId + "' and del_flag = '0' order by create_time desc limit 1 offset 0";
            return jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(DisasterScenarioManageEntity.class));
        } catch (EmptyResultDataAccessException e) {
            return null;
        }
    }

    @Override
    public Integer countByCounty(String p, String city, String county) {
        String sql = String.format("select count(*) from ypg_disaster_scenario_manage where province = '%s' and city = '%s' and area = '%s' and del_flag = '0'", p, city, county);
        return jdbcTemplate.queryForObject(sql, Integer.class);
    }
}
